package jue;

public class Lock {
    static Object lock1=new Object(),lock2=new Object();
    static void f1(){
        System.out.println(Thread.currentThread()+":等待锁1...");
        synchronized (lock1){
            System.out.println(Thread.currentThread()+":获得锁1，等待锁,2...");
            synchronized (lock2){
                System.out.println(Thread.currentThread()+":获得锁2");
            }
            System.out.println(Thread.currentThread()+":释放锁2");
        }
        System.out.println(Thread.currentThread()+":释放锁1");
    }
    static void f2(){
        System.out.println(Thread.currentThread()+":等待锁2...");
        synchronized (lock2){
            System.out.println(Thread.currentThread()+":获得锁2，等待锁,1...");
            synchronized (lock1){
                System.out.println(Thread.currentThread()+":获得锁1");
            }
            System.out.println(Thread.currentThread()+":释放锁1");
        }
        System.out.println(Thread.currentThread()+":释放锁2");
    }

    public static void main(String[] args) {
        new Thread(()->{f1();}).start();
        new Thread(()->{f2();}).start();
    }
}
